
class.save_ui2 = extends(class.panel) {
    reload = true,
    new = function (parent,x,y,w,h)
        local panel = class.save_ui2:builder 
        {
            parent = parent,
            normal_image = [[ui\天赋树\天赋树框底.tga]],
            x = x,
            y = y,
            w = w,
            h = h,
        }
        return panel
    end,
    init = function (self)
        class.panel.init(self)
        
        --self.title = self:add_text('|cffffff00天赋树|r',0,5,nil,30,16,5)
        self.title = self:add_panel([[ui\天赋树\天赋树.tga]],(self.w - 116)/2,25,116,40)

        --[横向经验分标题]
        local exp_count = 10
        local exp_gap_w = 80
        local exp_gap_h = 100
        local exp_w = 24
        local exp_h = 24
        local exp_line = self:add_panel([[ReplaceableTextures\TeamColor\TeamColor09.blp]],20,100,exp_gap_w*(exp_count-1),5)
        local exp_load = exp_line:add_panel([[ReplaceableTextures\TeamColor\TeamColor05.blp]],0,0,exp_line.w,exp_line.h)
        local titles = {}
        for a=1,exp_count do
            local panel = exp_line:add_panel([[textures\black32.blp]],(a-1)*exp_gap_w,(exp_line.h - exp_h)/2,exp_w,exp_h)
            panel.text = panel:add_text(a*10,-20,0,panel.w + 40,nil,8,5)
            titles[a] = panel
        end
        exp_load.max_w = exp_load.w
        self.exp_load = exp_load
        self.xpload = titles

        self.point = self:add_text('天赋点: 0|n已使用: 0',20,exp_line.y + exp_line.h + 10,100,40,8,4)
        --[设置经验标题和天赋内容]
        self.btn_w = 42
        self.btn_h = 42

        local xps = {0,5,10,20,40,70,100,130,160,200}
        local data = load_excel('存档数据.xlsx','天赋页存档')
        local buttons = {}

        for a,panel in ipairs(self.xpload) do
            panel.count = xps[a] or 0
            panel.text:set_text(xps[a] or '暂无')
        end
        
        local search = {}
        for i,info in ipairs(data) do
            local a,b = info.x,info.y
            local name = info.name or info['对应']
            local button = exp_line:add_tfBtn((a-1)*exp_gap_w,(b-1)*exp_gap_h + 150,self.btn_w,self.btn_h)
            button:set_normal_image(info.art or [[ReplaceableTextures\CommandButtons\BTNHero.blp]])
            button.name = name
            button.skill = info
            search[name] = button
            buttons[i] = button
        end
        
        --先出处理线路
        for i,info in ipairs(data) do
            local link = info.link or {}
            if type(link)=='string' then
                link = split_string(link,',')
            end
            local button = buttons[i]
            button.link = link
            
            for _,name in ipairs(link) do
                button:add_line(name,search[name])
            end
        end

        self.buttons = buttons
        self.show_data = data
        self:update()
        return self
    end,
        
    update = function(self)
        local player = ac.player.self
        local data = self.show_data or {}
        local search = {}
        local upgrade = player:get_data('天赋树-节点等级') or {} --保存等级
        local sum = player:get_data('天赋树-总等级') or 0
        local use = player:get_data('天赋树-使用点') or 0
        local point = player:get_data('天赋树-天赋点') or 0
        self.point:set_text(('|cff999999天赋点:|r %0.f|n|cff999999已使用:|r %0.f|n|cff999999总等级:|r %0.f'):format(point,use,sum))
        --self.xpload[1].text:set_text(sum)
        local xpload = 0
        local sumlv = 0
        local open = false
        for i,panel in ipairs(self.xpload) do
            local count = panel.count or 0
            if sum >= count then
                xpload = panel.x + panel.w
                sumlv = count
                panel.text:set_color(0xff00ff00)
            elseif sum < count then
                panel.text:set_color(0xffffffff)
                if open==false then
                    open = true
                    xpload = xpload + (sum-sumlv)/(count - sumlv)*(panel.x - xpload)
                end
            end
        end
        self.exp_load:set_width(xpload)

        for i,button in ipairs(self.buttons or {}) do
            button:reset_show()
        end
        
        --显示白线
        for i,button in ipairs(self.buttons or {}) do
            local info = data[i]
            local level = upgrade[i] or 0
            button:set_overlay(level)
            search[button.name] = button.open
        end
        
        --显示黄线
        for i,button in ipairs(self.buttons or {}) do
            local link = button.link
            for _,name in ipairs(link) do
                if search[name] then
                    button:show_line(name)
                end
            end
        end
    end,

    on_button_mouse_enter = function(self,button)
        local skill = button.skill
        if skill then
            button:tooltip(skill.title,skill.tip,'bottom',400)
            --button:tooltip(skill:get_title(),skill:get_tip())
        end
    end,

    on_button_clicked = function(self,button)
        local name = button.name
        if name then
            ac.sync('同步-天赋树加点',button.name)
        end
    end,

    on_button_right_clicked = function(self,button)
        local name = button.name
        if name then
            ac.sync('同步-天赋树加点',button.name,10)
        end
    end,
}

-- local w,h = 1000,600
-- local ui = class.save_ui2:builder 
-- {
--     x = (1920 - w)/2,
--     y = 150,
--     w = w,
--     h = h,
-- }